﻿// -----------------------------------------------------------------------
// <copyright file="Weapon.cs" company="Diddly Squad">
// </copyright>
// -----------------------------------------------------------------------
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using System;
using System.Collections.Generic;

namespace DiddlySquat
{
	/// <summary>
	/// Base class for all the weapons
	/// </summary>
	public abstract class Weapon
	{
        protected World World;
		public Body Body { get; set; }
        public string SpriteFileName { get; set; }
		public Texture2D Sprite { get; set; }
		public Texture2D ProjectileSprite { get; set; }
		public Vector2 BodyPosition { get; set; }
		public float Speed { get; set; }
		public Vector2 Direction { get; set; }
		public float Mass { get; set; }
        protected SoundEffect ShootingSound;

		public Animation WeaponAnimation { get; private set; }
		public Vector2 Position { get; private set; }
		public bool Active { get; private set; }

		/// <summary>
		/// Initializes the weapon.
		/// </summary>
		/// <param name="world">The world.</param>
		/// <param name="position">The position.</param>
		/// <param name="weaponSprite">The weapon sprite.</param>
		/// <param name="projectileSprite">The projectile sprite.</param>
		/// <param name="shootingSound">The shooting sound.</param>
		public void Initialize(World world, Vector2 position, Texture2D weaponSprite, Texture2D projectileSprite, SoundEffect shootingSound)
		{
			this.Position = position;
			this.World = world;
			this.ShootingSound = shootingSound;

			this.Body = BodyFactory.CreateRectangle(world, weaponSprite.Bounds.Width / Constants.MeterInPixels, weaponSprite.Bounds.Height / Constants.MeterInPixels, 1f, position);
			this.Body.BodyType = BodyType.Dynamic;

			this.Body.CollisionCategories = Constants.WeaponCollisionCategory;
			this.Body.CollidesWith = Constants.WeaponCollidesWith;

			this.Sprite = weaponSprite;
			this.ProjectileSprite = projectileSprite;

			this.Active = true;

			this.Body.FixedRotation = true;
		}

		/// <summary>
		/// Turns weapon to specified direction.
		/// </summary>
		/// <param name="faceThis">The Direction.</param>
		/// <param name="cameraPosition">The camera position.</param>
		public void TurnToFace(Vector2 faceThis, Vector2 cameraPosition)
		{
			Vector2 position = this.Body.Position * Constants.MeterInPixels + 
				new Vector2(cameraPosition.X + DiddlySquat.ScreenWidth * 
					Constants.PlayerOnCameraWidthRatio, cameraPosition.Y + DiddlySquat.ScreenHeight * Constants.PlayerOnCameraHeightRatio);

			var x = faceThis.X - position.X;
			var y = faceThis.Y - position.Y;

			this.Direction = new Vector2(x, y);
			this.Direction.Normalize();

			var desiredAngle = (float)Math.Atan2(y, x);
			this.Body.Rotation = desiredAngle;
		}

		/// <summary>
		/// Update the Projectile animation
		/// </summary>
		/// <param name="gameTime">The game time.</param>
		public void Update(GameTime gameTime)
		{
			WeaponAnimation.Position = Position;
			WeaponAnimation.Update(gameTime);
		}


		/// <summary>
		/// Fires the specified projectiles.
		/// </summary>
		/// <param name="projectiles">The projectiles.</param>
		/// <param name="soundLevel">The sound level.</param>
        public virtual void Fire(List<Projectile> projectiles, int soundLevel) { }

        public virtual void Disable()
        {
            this.Active = false;
            this.Body.FixedRotation = false;
        }

		/// <summary>
		/// Draws the weapon.
		/// </summary>
		/// <param name="spriteBatch">The sprite batch.</param>
		public void Draw(SpriteBatch spriteBatch)
		{
            spriteBatch.Draw(this.Sprite, this.Body.Position * Constants.MeterInPixels, null, Color.White, this.Body.Rotation, new Vector2(this.Sprite.Bounds.Width / 2, 0), 1f, SpriteEffects.None, 0f);
		}

	}
}
